﻿using Microsoft.Practices.Unity.InterceptionExtension;
using Sapit.Attributes.Interfaces;

namespace Sapit.Unity.Attributes
{
    /// <summary>
    /// Apply this attribute to methods where calls should be throttled when response times exceeds a certain threshold.
    /// </summary>
    public class ResponseTimeThrottleAttribute : HandlerAttribute, IResponseTimeThrottle
    {
        /// <summary>
        /// The threshold to trigger call throttling
        /// </summary>
        public double ThrottleThresholdMilliseconds { get; set; }
        /// <summary>
        /// The number of calls to skip when throttling
        /// </summary>
        public int SkipCallsWhenThrottling { get; set; }
        /// <summary>
        /// Defines the behavior when throttling, return a default value or throw an exception
        /// </summary>
        public Behavior BehaviorWhenThrottling { get; set; }
        /// <summary>
        /// The value to return when skipping calls
        /// </summary>
        public object ThrottleReturnValue { get; set; }

        public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
        {
            return new ResponseTimeThrottleHandler();
        }
    }
}
